package lz.controls
{
	import com.common.utils.DisplayObjectUtil;
	import com.common.utils.Reflection;
	import com.global.GlobalDef;
	
	import flash.display.DisplayObject;
	import flash.display.InteractiveObject;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.ui.Mouse;

	/**
	 * Mouse Cursor
	 * @author lake
	 * 
	 */
	public class MouseCursor extends Sprite
	{
		public static var cursor:Sprite;
		public static var _isShow:Boolean;
		
		public function MouseCursor()
		{
			super();
		}
		public static function setCursor(cls:Object,alpha:Number = 1):DisplayObject
		{
			if(cursor==null)
			{
				cursor = new Sprite();
			}
			else
			{
				DisplayObjectUtil.removeAllChildren(cursor);
			}
			var disobj :DisplayObject;
			cursor.alpha=alpha;
			if(cls is Class)
			{
				disobj = new cls();
			}
			else if(cls is String)
			{
				if(String(cls).indexOf("http") != -1)
				{
					var imgContain :ImageContain = new ImageContain();
					imgContain.url = String(cls);
					disobj = imgContain;
				}
				else
				{
					var child:* = Reflection.createInstance(String(cls));
					disobj = child;
				}
			}
			else if(cls is DisplayObject)
			{
				disobj = DisplayObject(cls);
			}
			if(disobj)
			{
				cursor.addChild(disobj);
			}
			if(cursor is InteractiveObject)
			{
				
				//cursor["mouseChildren"]=false;
				//cursor["mouseEnabled"]=false;
			}
			showCursor();
			return disobj;
		}
		private static function addedHandler(e:Event):void
		{
			var stage:Stage = GlobalDef.GameStage;
			stage.addChild(cursor);
		}
		private static function mouseMoveHandler(e:MouseEvent):void
		{
			var stage:Stage = GlobalDef.GameStage;
			cursor.x = stage.mouseX - 1;
			cursor.y = stage.mouseY - 1;
			e.updateAfterEvent();
		}
		private static function rollOutHandler(e:MouseEvent):void
		{
			Mouse.hide();
			var stage:Stage = GlobalDef.GameStage;
			stage.addChild(cursor);
		}
		private static function rollOverHandler(e:MouseEvent):void
		{
			if(e.target is TextField && TextField(e.target).selectable==true)
			{
				Mouse.show();
				var stage:Stage = GlobalDef.GameStage;
				if(stage.contains(cursor))stage.removeChild(cursor);
			}
			
		}
		public static function showCursor():void
		{
			_isShow=true;
			Mouse.hide();
			var stage:Stage = GlobalDef.GameStage;
			stage.addChild(cursor);
			cursor.x = stage.mouseX;
			cursor.y = stage.mouseY;
			stage.addEventListener(Event.ADDED,addedHandler);
			stage.addEventListener(MouseEvent.MOUSE_MOVE,mouseMoveHandler);
			stage.addEventListener(MouseEvent.MOUSE_OVER,rollOverHandler);
			stage.addEventListener(MouseEvent.MOUSE_OUT,rollOutHandler);
		}
		public static function hideCursor():void
		{
			_isShow=false;
			Mouse.show();
			var stage:Stage = GlobalDef.GameStage;
			
			if(cursor && cursor.parent)
			{
				cursor.parent.removeChild(cursor);
				cursor = null;
			}			
			stage.removeEventListener(Event.ADDED,addedHandler);
			
			stage.removeEventListener(MouseEvent.MOUSE_MOVE,mouseMoveHandler);
			stage.removeEventListener(MouseEvent.MOUSE_OVER,rollOverHandler);
			stage.removeEventListener(MouseEvent.MOUSE_OUT,rollOutHandler);
		}
	}
}